package com.wwm.petadopt;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;

public class HorizontalListViewAdapter extends BaseAdapter{
	private int[] mIconIDs;
	private String[] mTitles;
	private Context mContext;
	private LayoutInflater mInflater;
ArrayList<PetDetail> petDetailArray ;
	Bitmap iconBitmap;
	private int selectIndex = -1;

	public HorizontalListViewAdapter(Context context, String[] titles, int[] ids){
		this.mContext = context;
		this.mIconIDs = ids;
		this.mTitles = titles;
		mInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	public void setData(ArrayList<PetDetail> petDetailArray) {
		this.petDetailArray = petDetailArray;
	}
	@Override
	public int getCount() {
		return 6;
	}
	@Override
	public Object getItem(int position) {
		return petDetailArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;
		if(convertView==null){
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.horizontal_listview_item, null);
			holder.mImage=(ImageView)convertView.findViewById(R.id.img_list_item);
			holder.mFirstLine=(TextView)convertView.findViewById(R.id.text_classificaiton_firstline);
			holder.mSecondLine=(TextView)convertView.findViewById(R.id.text_classificaiton_secondline);
			convertView.setTag(holder);
		}else{
			holder=(ViewHolder)convertView.getTag();
		}
		if(position == selectIndex){
			convertView.setSelected(true);
		}else{
			convertView.setSelected(false);
		}
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(convertView.getContext())
//				// You can pass your own memory cache implementation
//				.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
//				.build();
//
		DisplayImageOptions options = new DisplayImageOptions.Builder()
				.displayer(new RoundedBitmapDisplayer(30)) //rounded corner bitmap
				.cacheInMemory(true)
				.cacheOnDisc(true)
				.build();

		ImageLoader imageLoader = ImageLoader.getInstance();
		//imageLoader.init(config);
		if (petDetailArray != null) {
			imageLoader.displayImage(petDetailArray.get(position).imageUrl, holder.mImage, options);
			holder.mFirstLine.setText(petDetailArray.get(position).petName);
			holder.mSecondLine.setText(petDetailArray.get(position).petVariety);
		}
		else
		{
//			imageLoader.displayImage("", holder.mImage, options);
			holder.mFirstLine.setText("加载中");
			holder.mSecondLine.setText("加载中");
		}
		return convertView;
	}

	private static class ViewHolder {
		private TextView mFirstLine ;
		private TextView mSecondLine;
		private ImageView mImage;
	}
	
	public void setSelectIndex(int i){
		selectIndex = i;
	}
}